﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Diagnostics;
using QuickTools.PluginUpdater;

namespace QuickTools.MatchmediaHelper
{
    public partial class FormUpdateAvailable : Form
    {
        private WirePluginUpdater updater;
        private UpdateCollection updates;
        private Update update;
        public FormUpdateAvailable(WirePluginUpdater updater, UpdateCollection updates, Update update)
        {
            this.update = update;
            this.updater = updater;
            this.updates = updates;

            update.ProgressChanged += new DownloadProgressChangedEventHandler(update_ProgressChanged);
            update.DownloadFinished += new EventHandler<UpdateDownloadFinishedEventArgs>(update_DownloadFinished);

            InitializeComponent();
            webBrowser1.Navigate(update.ChangelogUrl.Replace("{Version}", update.VersionString));
            lAvailableVersion.Text = String.Format("{0} ({1})", update.Version, update.Channel);
            lCurrentVersion.Text = updater.CurrentVersion.ToString();
            lProductName.Text = updates.Product;
        }

        void update_DownloadFinished(object sender, UpdateDownloadFinishedEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler<UpdateDownloadFinishedEventArgs>(update_DownloadFinished), sender, e);
            }
            else
            {
                bInstall.Enabled = true;
                if (e.HasError)
                {
                    MessageBox.Show(String.Format(Properties.Resources.UpdateErrorText, e.Error.Message), Properties.Resources.UpdateErrorTitle);
                }
                else
                {
                    try
                    {
                        Process.Start(e.DownloadedFile);
                        MessageBox.Show(Properties.Resources.UpdateInstallingText, Properties.Resources.UpdateInstallingTitle);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(String.Format(Properties.Resources.UpdateErrorText, ex.Message), Properties.Resources.UpdateErrorTitle);
                    }
                }
            }
        }

        void update_ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (pbDownload.InvokeRequired)
            {
                pbDownload.Invoke(new DownloadProgressChangedEventHandler(update_ProgressChanged), sender, e);
            }
            else
            {
                pbDownload.Value = e.ProgressPercentage;
            }
        }

        private void bInstall_Click(object sender, EventArgs e)
        {
            bInstall.Enabled = false;
            update.DownloadAsync();
        }
    }
}
